//
//  DDBaseTableViewController.m
//  DingDongZP
//
//  Created by Jyh on 16/3/13.
//  Copyright © 2016年 iyhjiang.com. All rights reserved.
//

#import "DDBaseTableViewController.h"
#import "MJRefresh.h"

@interface DDBaseTableViewController ()

/**
 *  判断tableView是否支持iOS7的api方法
 *
 *  @return 返回预想结果
 */
- (BOOL)validateSeparatorInset;

@end

@implementation DDBaseTableViewController

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.lock = [[NSLock alloc] init];
        self.startIndexPage = 0;
        self.pageCount = 20;
        
        //初始化设置TableView样式为Group
        self.tableViewStyle = UITableViewStyleGrouped;
    }
    
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    self.tableView.backgroundColor = COLOR_BACKGROUND_GRAY;
    
    self.navigationController.navigationBar.translucent = false;
    self.navigationController.navigationBar.barTintColor = COLOR_NAVIGATIONBAR;
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor whiteColor],NSFontAttributeName:[UIFont systemFontOfSize:16]}];
    
    
    [self.view addSubview:self.tableView];
    WEAKSELF
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(weakSelf.view);
        make.edges.mas_offset(UIEdgeInsetsZero);
    }];
    [self setNodataView];
}

-(void)viewWillAppear:(BOOL)animated {
    
//    self.navigationController.navigationBar.translucent = false;
//    self.navigationController.navigationBar.barTintColor = COLOR_NAVIGATIONBAR;
//    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
//    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor whiteColor],NSFontAttributeName:[UIFont systemFontOfSize:16]}];
}

//-(UIBarButtonItem *)customBackItemWithTarget:(id)target action:(SEL)action {
//    
//    UIBarButtonItem *btn = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"navBack.png"] style:UIBarButtonItemStyleDone target:self action:@selector(doBack)];
//    
//    return  btn;
//    
//}
//
//
//-(void)doBack {
//    
//    [self.navigationController popViewControllerAnimated:YES];
//}

- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}

- (BOOL)prefersStatusBarHidden
{
    return NO;
}

/**
 *  设置无数据View
 */
- (void)setNodataView
{
    if (!self.noDataView) {
        self.noDataView = [[UIView alloc] init];
        self.noDataView.backgroundColor = [UIColor clearColor];
        
        self.noDataImageView = [[UIImageView alloc] init];
        self.noDataImageView.frame = CGRectMake(1, 1, 1, 1);
        self.noDataImageView.center = self.view.center;
        
        self.noDataLabel = [[UILabel alloc] init];
        self.noDataLabel.textColor = COLOR_FONT_DARK_GRAY;
        self.noDataLabel.numberOfLines = 0;
        self.noDataLabel.textAlignment = NSTextAlignmentCenter;
        self.noDataLabel.font = [UIFont fontWithName:@"Helvetica" size:16.];
        
        [self.noDataView addSubview:self.noDataImageView];
        [self.noDataView addSubview:self.noDataLabel];
    }
    
    
    [self.view addSubview:self.noDataView];
    self.noDataView.hidden = YES;
}

#pragma mark - Propertys

- (UITableView *)tableView {
    if (!_tableView) {
        CGRect tableViewFrame = self.view.bounds;
        tableViewFrame.size.height -= (self.navigationController.viewControllers.count > 1 ? 64 : (CGRectGetHeight(self.tabBarController.tabBar.bounds)));
        _tableView = [[UITableView alloc] initWithFrame:tableViewFrame style:self.tableViewStyle];
        
        _tableView.delegate = self;
        _tableView.dataSource = self;
        if (![self validateSeparatorInset]) {
            if (self.tableViewStyle == UITableViewStyleGrouped) {
                //                UIView *backgroundView = [[UIView alloc] initWithFrame:_tableView.bounds];
                //                backgroundView.backgroundColor = _tableView.backgroundColor;
                //                _tableView.backgroundView = backgroundView;
            }
        }
        _tableView.backgroundColor = COLOR_BACKGROUND;
    }
    return _tableView;
}

- (NSMutableArray *)dataSource {
    if (!_dataSource) {
        _dataSource = [[NSMutableArray alloc] initWithCapacity:1];
    }
    return _dataSource;
}


#pragma mark
#pragma mark - Public Methods

- (void)configuraTableViewNormalSeparatorInset {
    if ([self validateSeparatorInset]) {
        [_tableView setSeparatorInset:UIEdgeInsetsZero];
    }
}

- (void)configuraSectionIndexBackgroundColorWithTableView:(UITableView *)tableView {
    if ([tableView respondsToSelector:@selector(setSectionIndexBackgroundColor:)]) {
        tableView.sectionIndexBackgroundColor = [UIColor clearColor];
    }
}

- (void)loadDataSource {
    // subClasse
}

- (void)addRefreshHeader
{
    // 添加传统的下拉刷新
    // 设置回调（一旦进入刷新状态，就调用target的action，也就是调用self的loadNewData方法）
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
}

- (void)addRefreshFooter
{
    // 添加传统的上拉刷新
    // 设置回调（一旦进入刷新状态，就调用target的action，也就是调用self的loadMoreData方法）
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
}

- (void)setFooterHidden:(BOOL)hidden
{
    self.tableView.mj_footer.hidden = hidden;
}

- (void)beginRefreshHeader
{
    [self.tableView.mj_header beginRefreshing];
}

- (void)endRefreshHeader
{
    [self.tableView.mj_header endRefreshing];
}

- (void)beginRefreshFooter
{
    [self.tableView.mj_footer beginRefreshing];
}

- (void)endRefreshFooter
{
    [self.tableView.mj_footer endRefreshing];
}

- (void)loadNewData
{
    
}

- (void)loadMoreData
{
    
}

- (BOOL)isRefreshing
{
    return [self.tableView.mj_header isRefreshing] || [self.tableView.mj_footer isRefreshing];
}


- (void)showNoDataViewWithImage:(UIImage *)image text:(NSString *)text
{
    if (!self.noDataView.hidden) {
        return;
    }
    
    self.noDataView.hidden = NO;
    
    if (image) {
        self.noDataImageView.image = image;
        
        float imageWidth = 0;
        float imageHeight = 0;
        if (image.size.width >400) {
            imageWidth = 400/2;
            imageHeight = image.size.height/image.size.width*200;
        } else {
            imageWidth = image.size.width;
            imageHeight = image.size.height;
        }
        
        [self.noDataImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.view.mas_centerX);
            make.centerY.equalTo(self.view.mas_centerY).offset(-200);
            make.width.equalTo([NSNumber numberWithFloat:imageWidth]);
            make.height.equalTo([NSNumber numberWithFloat:imageHeight]);
        }];
        
        if (text && [text length] > 0) {
            self.noDataLabel.text = text;
            
            [self.noDataLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerX.equalTo(self.view.mas_centerX);
                make.centerY.equalTo(self.noDataImageView.mas_bottom).offset(0);
                make.left.equalTo(self.view.mas_left).offset(20);
                make.right.equalTo(self.view.mas_right).offset(-20);
            }];
        }
    } else {
        if (text && [text length] > 0) {
            self.noDataLabel.text = text;
            
            [self.noDataLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerX.equalTo(self.view.mas_centerX);
                make.centerY.equalTo(self.view.mas_centerY);
                make.left.equalTo(self.view.mas_left).offset(20);
                make.right.equalTo(self.view.mas_right).offset(-20);
            }];
        }
    }

}

- (void)hideNoDataView
{
    self.noDataView.hidden = YES;
}

#pragma mark - TableView Helper Method

- (BOOL)validateSeparatorInset {
    if ([_tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        return YES;
    }
    return NO;
}

#pragma mark - UITableView DataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // in subClass
    return nil;
}

#pragma mark - UITableView Delegate

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.5;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.5;
}


#pragma mark - Touch event
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
    viewController.hidesBottomBarWhenPushed = YES;
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
    [self.navigationController pushViewController:viewController animated:animated];
}


@end
